package com.lky.reggie.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @author shkstart
 * @Description
 * @date 2023-01-03-15:29
 */
@Deprecated
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String requestURL = request.getRequestURI();// /backend/index.html

        log.info("拦截请求的路径是：",requestURL);
        //登录检查
        HttpSession session = request.getSession();

        Object user = session.getAttribute("employee");
        if(user!=null)
        {
            log.info("登录成功");

            long id = Thread.currentThread().getId();
            log.info("线程id为：{}",id);
            //放行
            return true;
        }
        //拦截 未登录，跳转登录页面
        request.setAttribute("employee","请先登录");
//        response.sendRedirect("/");
        request.getRequestDispatcher("/").forward(request,response);
        return false;


    }

    /**
     * 目标方法完成以后
     * @param request
     * @param response
     * @param handler
     * @param modelAndView
     * @throws Exception
     */
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        log.info("postHandle执行：",modelAndView);

    }

    /**
     *页面渲染以后
     * @param request
     * @param response
     * @param handler
     * @param ex
     * @throws Exception
     */
    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        log.info("afterCompletion执行异常：",ex);
    }
}
